package com.vhall.component.service.room.event.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * spring bean 处理器
 *
 * @author jingwen.li
 * @date 2023/2/16
 */
@Slf4j
@Component
public class HandlerProcessor implements BeanFactoryPostProcessor {

    private final String prefix = "com.vhall.component.service.room.event.handler";

    private final String documentPackage = prefix + ".document";
    private final String inavPackage = prefix + ".inav";
    private final String livePackage = prefix + ".live";
    private final String notifyPackage = prefix + ".notify";
    private final String recordPackage = prefix + ".record";

    private final String[] basePackages = new String[]{documentPackage, inavPackage, livePackage, notifyPackage, recordPackage};

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Map<String, Class> map = new HashMap<>();

        ClassScaner.scan(basePackages, EventHandlerType.class).forEach(x -> {
            String type = x.getAnnotation(EventHandlerType.class).value();
            map.put(type, x);
        });

        beanFactory.registerSingleton(EventHandlerType.class.getName(), map);

        log.info("事件处理器初始化完毕");
    }
}
